import { config } from '@/config/global.config'

async function preventDuplicateSubmit (preventBtnFun, callback, preventBtnDuration) {
  preventBtnFun(true)
  try {
    if (callback) {
      await callback()
    }
  } finally {
    window.setTimeout(() => {
      preventBtnFun(false)
    }, preventBtnDuration || config.preventBtnDuration)
  }
}

function dateFormat (date) {
  const dt = new Date(date)
  const y = dt.getFullYear()
  const m = (dt.getMonth() + 1).toString().padStart(2, '0')
  const day = dt.getDate().toString().padStart(2, '0')
  const hh = dt.getHours().toString().padStart(2, '0')
  const mm = dt.getMinutes().toString().padStart(2, '0')
  const ss = dt.getSeconds().toString().padStart(2, '0')
  return `${y}-${m}-${day} ${hh}:${mm}:${ss}`
}

export default {
  preventDuplicateSubmit,
  dateFormat
}
